package com.kingyea.mobilepolice.apimanage.camel;

import com.alibaba.fastjson.JSON;
import com.kingyea.camel.runtime.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author Mr.Lin
 * @version v1.0.0
 * @description   用于测试kingyeaCamel 的状态控制
 * @date Created in 2018/9/14 23:16
 */

@RequestMapping(value = "/camel")
@RestController
public class Controller4TestKingyeaCamel {

//    @Autowired
//    KingyeaCamelThread kingyeaCamelThread;

    @GetMapping("hello")
    public Object hello(){
        System.out.println("hello world");
        return "hello world";
    }

    @GetMapping("loadService")
    public Object loadService(String path) throws Exception {
        path = StringUtils.isEmpty(path)?"D:\\A\\get_ftp_file.xml":path;
        Service service = KingyeaCamelService.loadService(path);
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>KingyeaCamelService:"+service);
        return null==service||StringUtils.isEmpty(service.getServiceId())?"加载不成功？":"id:"+service.getServiceInfo().getServiceId()+", name:"+service.getServiceInfo().getServiceName();
    }

    @ResponseBody
    @GetMapping("getAll")
    public Object getCamelServiceMap(){
        return JSON.toJSONString(KingyeaCamelService.getServiceContainer().listServiceInfos());
    }

    @GetMapping("stop/{id}")//停止
    public Object stopService(@PathVariable(value = "id") String id) throws Exception {
        System.out.println("stopService");
        KingyeaCamelService.getServiceContainer().stopService(id);
        return KingyeaCamelService.getServiceContainer().getServiceInfoById(id).getStatus();
    }

    @GetMapping("start/{id}")
    public Object startService(@PathVariable(value = "id") String id) throws Exception {
        System.out.println("startService");
        KingyeaCamelService.getServiceContainer().startService(id);
        return KingyeaCamelService.getServiceContainer().getServiceInfoById(id).getStatus();
    }


    @GetMapping("destory")
    public Object destoryService(@PathVariable(value = "id") String id) throws Exception {
        System.out.println("destory ServiceContainer");
        KingyeaCamelService.getServiceContainer().destory();
        return "ok";
    }

    @GetMapping("suspend/{id}")//暂停、挂起
    public Object suspendService(@PathVariable(value = "id") String id) throws Exception {
        System.out.println("suspendService");
        KingyeaCamelService.getServiceContainer().suspendService(id);
        return KingyeaCamelService.getServiceContainer().getServiceInfoById(id).getStatus();
    }




}
